// pages/article/channel/channel.js
const app = getApp()
const util = require("../../../utils/util.js");
var page = 1;
Page({

    /**
     * 页面的初始数据
     */
    data: {
        pindex: 0,
        menuIndex: 0,
        lanmuIndex: 0,
        channelData: [],
        channelList: [],
        classList: [],
        lanmuLsit: [],
        articleList: [],

        topHeight: 0,
        menuHeight: 0,
        lanmuHeight: 0,

        showLoading: false,
        shownull: false,
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.setData({
            siteid: options.id ? options.id : '',
            type: options.type ? options.type : '',
            citycode: options.city ? options.city : (ks.getStorageSync("citycode") ? ks.getStorageSync("citycode") : 'sz'),
        })
        util.getUrl(app.globalData.url).then((res) => {
            util.cityList().then((res) => {
                let citydata = res.allcity.filter(v => v.citycode == this.data.citycode);
                this.setData({
                    city: citydata[0].cityname
                })
                this.getInfoData();
                this.getlmArticle(this.data.siteid, page)
            })

        })

        //获取胶囊位置信息
        let menuButtonObject = ks.getMenuButtonBoundingClientRect();
        this.setData({
            navtop: menuButtonObject.top,
            inputHeight: menuButtonObject.height,
        })

        var query = ks.createSelectorQuery()
        query.select('.navbar').boundingClientRect((res) => {
            this.setData({
                topHeight: res.height,
            });
        }).exec()
    },

    // 其他频道内容
    getInfoData() {
        let data = {
            citycode: this.data.citycode,
            action: 'indexlist',
            pagesize: 4,
            version: app.globalData.version,
        };
        util.request('index.php', data, 'GET').then(res => {
            let infores = res.data;
            this.setData({
                channelList: infores,
            })
            infores.forEach((v, k) => {
                this.data.channelData.push(v.wapname)
                this.setData({
                    channelData: this.data.channelData,
                })

                if (v.id == this.data.siteid) {
                    this.setData({
                            pindex: k,
                        })
                        // this.data.pindex = k;
                    this.getclassData(this.data.siteid)
                }
            })
        })
    },

    // 获取栏目接口
    getclassData(id) {
        let siteData = {
            citycode: this.data.type == 'wenda' ? 'wenda' : ((this.data.type == 'bsy' && this.data.citycode == 'sz') ? 'bsy' :
                this.data.citycode),
            action: 'classlist',
            siteid: id,
            version: app.globalData.version,
        };
        util.request('index.php', siteData, 'GET').then(res => {
            if (res.data) {
                res.data.forEach((v, k) => {
                    v.checked = false;
                })
                this.setData({
                    classList: res.data,
                })
            }
            setTimeout(res => {
                var query = ks.createSelectorQuery()
                query.select('.scolls').boundingClientRect((res) => {
                    this.setData({
                        menuHeight: (res != null) ? res.height + 10 : 0,
                    });
                }).exec()
            }, 10);
        })
    },

    // 获取子栏目接口
    getlanmuData(id) {
        let siteData = {
            citycode: this.data.type == 'wenda' ? 'wenda' : ((this.data.type == 'bsy' && this.data.citycode == 'sz') ? 'bsy' :
                this.data.citycode),
            action: 'classlist',
            classid: id,
        };
        util.request('index.php', siteData, 'GET').then(res => {
            if (res.data) {
                res.data.forEach((v, k) => {
                    v.checked = false;
                })
                this.setData({
                    lanmuLsit: res.data,
                })

                var query = ks.createSelectorQuery()
                query.select('.lanmus').boundingClientRect((res) => {
                    this.setData({
                        lanmuHeight: (res != null) ? res.height : 0,
                    });
                }).exec()
            }
        })
    },

    // 获取栏目文章列表
    getlmArticle(siteid, page) {
        let infoData = {
            citycode: this.data.type == 'wenda' ? 'wenda' : ((this.data.type == 'bsy' && this.data.citycode == 'sz') ? 'bsy' :
                this.data.citycode),
            action: 'list',
            city: this.data.city,
            pagesize: 15,
            siteid: siteid,
            page: page,
        };
        util.request('index.php', infoData, 'GET').then(res => {
            this.setData({
                articleList: this.data.articleList.concat(res.data),
            })
            this.setData({
                showLoading: false,
            })

            if (this.data.articleList == "") {
                this.setData({
                    shownull: true,
                })
            }
        })
    },

    // 获取子栏目文章列表
    getzlmArticle(classid, page) {
        let infoData = {
            citycode: this.data.type == 'wenda' ? 'wenda' : ((this.data.type == 'bsy' && this.data.citycode == 'sz') ? 'bsy' :
                this.data.citycode),
            action: 'list',
            city: this.data.city,
            pagesize: 15,
            classid,
            page,
        };
        util.request('index.php', infoData, 'GET').then(res => {
            this.setData({
                articleList: this.data.articleList.concat(res.data),
            })
            this.setData({
                showLoading: false,
            })

            if (this.data.articleList == "") {
                this.setData({
                    shownull: true,
                })
            }
        })
    },

    // picker
    bindPickerChange(e) {
        this.setData({
            pindex: e.detail.value,
        })
        let wapname = this.data.channelData[this.data.pindex];
        this.data.channelList.forEach((v, k) => {
            if (v.wapname == wapname) {
                this.setData({
                    type: v.pagetype,
                    lanmuHeight: "",
                    lanmuLsit: [],
                    articleList: [],
                    classid: '',
                })
                this.getclassData(v.id)
                this.getlmArticle(v.id, page)
            }
        })
    },

    menuClick(e) {
        let id = e.currentTarget.dataset.id;
        let classname = e.currentTarget.dataset.classname;
        let index = e.currentTarget.dataset.index;
        this.data.classList.forEach((v, k) => {
            if (index == k) {
                v.checked = true;
            } else {
                v.checked = false;
            }
        })
        this.setData({
            classList: this.data.classList,
            articleList: [],
            classid: id,
            classname: classname,
            showLoading: true,
            shownull: false,
        })
        this.getzlmArticle(id, 1)
        this.getlanmuData(id)

    },

    lanmuClick(e) {
        let id = e.currentTarget.dataset.id;
        let classname = e.currentTarget.dataset.classname;
        let index = e.currentTarget.dataset.index;
        this.data.lanmuLsit.forEach((v, k) => {
            if (index == k) {
                v.checked = true;
            } else {
                v.checked = false;
            }
        })
        this.setData({
            lanmuLsit: this.data.lanmuLsit,
            articleList: [],
            classid: id,
            classname: classname,
            showLoading: true,
            shownull: false,
        })
        this.getzlmArticle(id, 1)
    },



    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {
        page++;
        if (this.data.classid != '') {
            this.getzlmArticle(this.data.classid, page)
        } else {
            this.getlmArticle(this.data.siteid, page)
        }
    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function() {

    }
})